import os
import re
import pandas as pd

result = []

with open('0719_data/bert2023_07_19_09_52_41.txt', 'r', encoding='utf-8') as f:
    sentences = f.readlines()
    sentences = [sen.replace('"', '') for sen in sentences]
# 监督数据
pattern_1 = r"(__label__|__title__|__content__|__app_name__)([^_]+)"
# 无监督数据
pattern_2 = r"(__title__|__content__|__app_name__)([^_]+)"
for sentence in sentences:
    sentence = sentence.replace('\n', '').strip()
    sentence = sentence.replace('\t', '')
    matches = re.findall(pattern_2, sentence)
    result.append(matches)
print(len(result))
# df = pd.DataFrame(columns=['app_name', 'title', 'content', 'label'])
# data = []
# for match in result:
#     # print("Label:{}, title:{}, content:{}, app_name:{}".format(match[0], match[1], match[2], match[3]))
#     # data.append({'label': match[0][1], 'title': match[1][1], "content": match[2][1], "app_name": match[3][1]})
#     data.append({"content": match[2][1], "app_name": match[3][1]})
#
# df = pd.DataFrame(data)
# if not os.path.exists('test_data'):
#     os.mkdir('test_data')
# df.to_csv("test_data/processed_data_2.csv.txt")

## 06/28 version
label_map_1 = {'服务提醒': 0, "内容资讯": 1, "营销活动": 2, "社交通讯": 3}
label_map_2 = {"私信-社交通讯-即时聊天": 0, "私信-服务提醒-订阅": 1, "私信-服务提醒-出行": 2, "私信-服务提醒-健康": 3, "私信-服务提醒-工作事项提醒": 4,
               "私信-服务提醒-帐号动态": 5, "私信-服务提醒-订单和物流": 6, "私信-服务提醒-财务": 7, "私信-服务提醒-设备提醒": 8, "私信-服务提醒-系统提示": 9,
               "私信-服务提醒-邮件": 10, "私信-服务提醒-正在发生的事": 11, "公信-内容资讯-内容推荐": 12, "公信-内容资讯-新闻": 13, "公信-内容资讯-财经动态": 14,
               "公信-内容资讯-生活资讯": 15,
               "公信-内容资讯-社交动态": 16, "公信-内容资讯-调研": 17, "公信-内容资讯-其他": 18, "公信-营销活动-产品促销": 19, "公信-营销活动-功能推荐": 20,
               "公信-营销活动-运营活动": 21}

label_map_3 = {"其他-其他-其他": 0, "私信-服务提醒-订阅": 1, "私信-服务提醒-正在发生的事": 2, "公信-内容资讯-内容推荐": 3, "公信-营销活动-产品促销": 4,
               "私信-社交通讯-即时聊天": 5}
with open('0719_data/bert_test_0719.txt', 'w', encoding='utf-8') as f:
    for match in result:
        # print("Label:{}, title:{}, content:{}, app_name:{}".format(match[0], match[1], match[2]))
        # data.append({'label': match[0][1], 'title': match[1][1], "content": match[2][1], "app_name": match[3][1]})
        # 1 用于提取训练、测试数据
        # label = match[0][1]
        # app_name = match[3][1]
        # f.write(app_name + "-" + match[2][1] + "\t" + str(label_map_3[label]) + "\n")
        # 2
        # inner_match = re.search(r"-(.+?)-", label)
        # if inner_match:
        #     inner_label = inner_match.group(1)
        #     f.write(app_name + "-" + match[2][1] + "\t" + str(label_map_1[inner_label]) + "\n")

        # 3 without label 用于提取测试数据
        print(match)
        title, content, app_name = match[0][1], match[1][1], match[2][1]
        f.write(app_name + "-" + content + "\t" + "\n")
print('Done!')